#include<stdio.h>
#include"jpg.h"
#include<stdio.h>
#include<dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include<stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/input.h>
#include"klist.h"
#define MOVE_DISTANCE 95

typedef struct jpeg_node{
	char *filename;
	char *rgbbuffer;
	struct image_info minfo;
	struct list_head list;
}*jpglist;
/*初始化JPG链表*/
jpglist init_jpglist()
{
	jpglist head=calloc(1,sizeof(struct jpeg_node));
	if(head!=NULL)
	{
		INIT_LIST_HEAD(&head->list);
	
	}
	return head;
}
/*创建JPG节点*/
jpglist create_node(char *filename,char *rgbdata)
{
	jpglist new=calloc(1,sizeof(struct jpeg_node));
	if(new!=NULL)
	{
		new->filename=filename;
		new->rgbbuffer=rgbdata;
		//new->minfo=NULL;
		INIT_LIST_HEAD(&new->list);
	}
	return new;
}
bool is_empty(jpglist head)
{
	if(head==NULL)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*读取jpg文件的文件名字，且创建节点*/
jpglist readFile(char *dirname,jpglist head)
{
	//打开目录
	if(!is_empty(head))
	{
		printf("链表为空\n");
		exit(-1);
	}
	DIR *dp=opendir(dirname);
	if(dp==NULL)
	{
		perror("打开目录失败");
	}
	chdir(dirname);
	struct dirent *readDir;
	while(1)
	{
		readDir=readdir(dp);
		if(readDir==NULL)
		{	break;	}
		if(readDir->d_name[0]=='.')//跳过当前目录
		{	continue;	}
		
		if(strstr(readDir->d_name,".jpg")==NULL)//跳过不是jpg后缀文件
		{	continue; 	}
		jpglist new=create_node(readDir->d_name,NULL);
		 list_add_tail(&new->list,&head->list);	
	}
	closedir(dp);
	return head;
}
/*显示下一张图片*/
struct list_head * show_jpg_next(struct list_head *p,int lcd,struct list_head *head)
{
	//struct list_head *q=p;
	//struct fb_var_screeninfo vinfo;
	//bzero(&vinfo,sizeof(vinfo));
	struct image_info minfo;
	bzero(&minfo,sizeof(minfo));	
	jpglist ptr;
	//头结点的下一个
	p=p->next; 
	
	if(p==head)//跳过头结点
	{	
		printf("没有下一张图片了\n");
		return p=p->prev;
		}				
	ptr=list_entry(p,struct jpeg_node,list);
	if(ptr->rgbbuffer==NULL)
	{
		ptr->rgbbuffer=load_file(ptr->filename, &minfo);
		ptr->minfo=minfo;
	}

	display(ptr->rgbbuffer,&ptr->minfo,lcd,0,0);
	//printf("翻到下一页\n");
	return p;	
}
/*显示上一张图片*/
struct list_head * show_jpg_prev(struct list_head *p,int lcd,struct list_head *head)
{
	//struct list_head *q=p;
	//struct fb_var_screeninfo vinfo;
	//bzero(&vinfo,sizeof(vinfo));
	struct image_info minfo;
	bzero(&minfo,sizeof(minfo));	
	jpglist ptr;
	
	p=p->prev;
					
	if(p==head) //跳过头结点
	{	
		printf("没有上一张图片了\n");
		return p=p->next;
		}
	ptr=list_entry(p,struct jpeg_node,list);
	if(ptr->rgbbuffer==NULL)
	{
		ptr->rgbbuffer=load_file(ptr->filename, &minfo);
		ptr->minfo=minfo;
	}
	display(ptr->rgbbuffer,&ptr->minfo,lcd,0,0);
	//printf("返回上一页\n");
	return p;

}
/*获取移动距离*/
int movepal(FILE *fp)
{
	int fx=0,lx=0;
	int dx;
	bool xflag=false;
	struct input_event buf;
	bzero(&buf,sizeof(buf));
	while(1)
	{
		xflag=false;
		while(1){
		fread(&buf,sizeof(buf),1,fp);
		
		if(buf.type==EV_ABS)
		{
			if(buf.code==ABS_X&&xflag==false)
			{
				fx=buf.value;			
				xflag=true;
			}
			else if(buf.code==ABS_X&&xflag==true)
			{
				lx=buf.value;
				//printf("lx:%d\n",lx);					
			}
		
		}
		
		 if(buf.type==EV_KEY)
		{

			if(buf.code==BTN_TOUCH)
			{
			
				if(buf.value==0)
				{
					dx=lx-fx;
					if(lx!=0)
					{
						return dx;
					}
					break;
				}
			}
		}
		}
	}
}
int main(int argc,char **argv)
{
	if(argc!=2)
	{
		printf("用法:%s <目录>\n",argv[0]);
		exit(-1);
	}


	jpglist head=init_jpglist();
	head=readFile(argv[1],head);
	
	int lcd=open("/dev/fb0",O_RDWR);
	if(lcd==-1)
	{
		perror("打开fb0文件失败");
		exit(-1);
	}
	FILE *fp=fopen("/dev/input/event0","r");
	if(fp==NULL)
	{
		perror("打开event0文件失败");
		exit(-1);
	}
	struct list_head *p=&head->list;
	p=show_jpg_next(p,lcd,&head->list);
	while(1)
	{
		int dx=movepal(fp);
		if(dx >= MOVE_DISTANCE)
		{		
			p=show_jpg_next(p,lcd,&head->list);
		}
		else if(dx <= -MOVE_DISTANCE)
		{		
			p=show_jpg_prev(p,lcd,&head->list);	
		}
		
	}
	
	return 0;
}
